package com.qqls.youxiangousys.pj.sys.service.impl;

import com.qqls.youxiangousys.pj.common.entity.Pagination;
import com.qqls.youxiangousys.pj.common.util.Assert;
import com.qqls.youxiangousys.pj.sys.dao.SysNoticeDao;
import com.qqls.youxiangousys.pj.sys.entity.SysNotice;
import com.qqls.youxiangousys.pj.sys.service.SysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysNoticeServiceImpl implements SysNoticeService {
    @Autowired
    private SysNoticeDao noticeDao;


    public Pagination findAll(String noticeContent, Integer curPage, Integer pageSize) {
        //验证参数
        Assert.isEmpty(curPage == null || pageSize == null,"请选择当前页码或者每页条数！！");
        //得到角色的总条数
        Integer n= noticeDao.getCount(noticeContent);
        Assert.isEmpty(n == 0,"查询错误");
        Pagination pageObj = new Pagination(curPage,n,pageSize);
        curPage = pageObj.getCurPage();//得到当前页
        pageSize = pageObj.getPageSize();//得到每页条数
        //分页跳过条数=（当前页 - 1）*每页条数
        Integer start = (curPage - 1) * pageSize;
        List<SysNotice> list=noticeDao.findNotice(noticeContent,start,pageSize);
        Assert.isEmpty(list==null || list.size()==0,"数据不存在或已被删除");
        pageObj.setPageData(list);
        return pageObj;
    }

    /**
     * 添加
     * @param notice
     * @return
     */
 public Integer addNotice(SysNotice notice) {
        Assert.isEmpty(notice==null || notice.getNoticeContent()==null,"请填写内容");
        SysNotice userByName = noticeDao.findNoticeByName(notice.getNoticeContent());
        Assert.isEmpty(userByName != null,"用户已存在");
        int i = noticeDao.addNotice(notice);
        Assert.isEmpty(i==0,"添加失败");
        return i;
    }

    //修改
    public Integer updateNotice(SysNotice notice) {
     Assert.isEmpty(notice == null || notice.getId() == 0,"请选择要修改的数据");
     int n=noticeDao.updateNotice(notice);
     Assert.isEmpty(n == 0,"修改失败");
     return n;
    }

    //状态
    public Integer updateValId(Integer id, Integer noticeState) {
     Assert.isEmpty(id == 0 || id == null,"请选择用户");
     Assert.isEmpty(noticeState == null,"操作有误");
     int n= noticeDao.updateValId(id,noticeState);
     Assert.isEmpty(n==0,"修改失败");
        return n;
    }


    public Integer deleteNotice(Integer[] ids) {
      Assert.isEmpty(ids.length==0||ids==null,"请你选择数据！");
      Integer n=noticeDao.deleteNotice(ids);
      Assert.isEmpty(n==0,"删除失败");
      return n;
    }
}
